package com.jhgsys.internal.workflow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jhgsys.internal.workflow.constant.WorkflowConstant;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.workflow.dto.*;
import com.jhgsys.internal.workflow.entity.*;
import com.jhgsys.internal.workflow.mapper.WorkflowInfoMapper;
import com.jhgsys.internal.workflow.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.*;

/**
 * 流程信息表 Service实现
 *
 * @author fenghaijun
 * @date 2024-07-15 17:34:52
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class WorkflowInfoServiceImpl extends ServiceImpl<WorkflowInfoMapper, WorkflowInfo> implements IWorkflowInfoService {

    @Autowired
    private IWorkflowNodeService workflowNodeService;

    @Autowired
    private IWorkflowNodeButtonService workflowNodeButtonService;

    @Autowired
    private IWorkflowNodeReviewService workflowNodeReviewService;

    @Autowired
    private IWorkflowNodeTransService workflowNodeTransService;

    @Autowired
    private IWorkflowNodeTransConditionService workflowNodeTransConditionService;

    /**
     * 查询（分页）
     * @author fenghaijun
     * @date 2024-07-15 17:34:52
     * @param request QueryRequest
     * @param workflowInfo workflowInfo
     * @return IPage<WorkflowInfo>
    */
    @Override
    public IPage<WorkflowInfo> findWorkflowInfos(QueryRequest request, WorkflowInfo workflowInfo) {
        LambdaQueryWrapper<WorkflowInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WorkflowInfo::getDeletedFlg, FebsConstant.VALID);
        if(StringUtils.isNotBlank(workflowInfo.getTenantId())){
            queryWrapper.eq(WorkflowInfo::getTenantId, workflowInfo.getTenantId());
        }
        if(StringUtils.isNotBlank(workflowInfo.getWorkflowName())){
            queryWrapper.like(WorkflowInfo::getWorkflowName, workflowInfo.getWorkflowName());
        }
        Page<WorkflowInfo> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author fenghaijun
     * @date 2024-07-15 17:34:52
     * @param workflowInfo workflowInfo
     * @return List<WorkflowInfo>
     */
    @Override
    public List<WorkflowInfo> findWorkflowInfos(WorkflowInfo workflowInfo) {
	    LambdaQueryWrapper<WorkflowInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WorkflowInfo::getDeletedFlg, FebsConstant.VALID);
        if(StringUtils.isNotBlank(workflowInfo.getTenantId())){
            queryWrapper.eq(WorkflowInfo::getTenantId, workflowInfo.getTenantId());
        }
        if(StringUtils.isNotBlank(workflowInfo.getWorkflowName())){
            queryWrapper.eq(WorkflowInfo::getWorkflowName, workflowInfo.getWorkflowName());
        }
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author fenghaijun
    * @date 2024-07-15 17:34:52
    * @param id
    * @return WorkflowInfo
    */
    @Override
    public WorkflowInfo findWorkflowInfoById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author fenghaijun
     * @date 2024-07-15 17:34:52
     * @param workflowInfo workflowInfo
     */
    @Override
    @Transactional
    public void createWorkflowInfo(WorkflowInfo workflowInfo) {
        workflowInfo.setCreatedTime(new Date());
        this.save(workflowInfo);
    }

    /**
     * 修改
     * @author fenghaijun
     * @date 2024-07-15 17:34:52
     * @param workflowInfo workflowInfo
     */
    @Override
    @Transactional
    public void updateWorkflowInfo(WorkflowInfo workflowInfo) {
        workflowInfo.setUpdatedTime(new Date());
        this.saveOrUpdate(workflowInfo);
    }

    /**
     * 逻辑删除
     * @author fenghaijun
     * @date 2024-07-15 17:34:52
     * @param workflowInfo workflowInfo
     * @param workflowInfoIds workflowInfoIds
     */
    @Override
    @Transactional
    public void deleteWorkflowInfo(WorkflowInfo workflowInfo,String workflowInfoIds) {
        List<String> list = Arrays.asList(workflowInfoIds.split(StringPool.COMMA));
        this.baseMapper.update(workflowInfo,new LambdaQueryWrapper<WorkflowInfo>().in(WorkflowInfo::getWorkflowId,list));
	}

    /**
    * 物理删除
    * @author fenghaijun
    * @date 2024-07-15 17:34:52
    * @param workflowInfoIds workflowInfoIds
    */
    @Override
    @Transactional
    public void physicallyDeleteWorkflowInfo(String workflowInfoIds) {
        List<String> list = Arrays.asList(workflowInfoIds.split(StringPool.COMMA));
        LambdaQueryWrapper<WorkflowInfo> wapper = new LambdaQueryWrapper<>();
        wapper.in(WorkflowInfo::getWorkflowId,list);
        this.remove(wapper);
    }

    /**
     * 保存工作流配置
     * @param workflowSaveRequest
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean workflowSave(WorkflowSaveRequestDto workflowSaveRequest)throws IOException {
        WorkflowInfo workflowInfo = workflowSaveRequest.getWorkflowInfo();
        Date date = new Date();
        String userName= workflowSaveRequest.getUserName();

        //#region 基础设置
        String versionId = FebsUtil.uuid();
        if(null!=workflowInfo.getWorkflowId()){
            //更新
            workflowInfo.setUpdatedBy(userName);
            workflowInfo.setUpdatedTime(date);
            updateById(workflowInfo);
        }else{
            //新增
            workflowInfo.setCreatedBy(userName);
            workflowInfo.setCreatedTime(date);
            save(workflowInfo);
        }
        //#endregion

        //#region 流程设计
        String workflowDesignJson = workflowInfo.getWorkflowJson();
        if(StringUtils.isNotBlank(workflowDesignJson)){

            //临时作废
            workflowNodeService.update(
                    new LambdaUpdateWrapper<WorkflowNode>().set(WorkflowNode::getDeletedFlg, FebsConstant.INVALID)
                            .eq(WorkflowNode::getWorkflowId,workflowInfo.getWorkflowId())
            );
            workflowNodeTransService.update(
                    new LambdaUpdateWrapper<WorkflowNodeTrans>().set(WorkflowNodeTrans::getDeletedFlg, FebsConstant.INVALID)
                            .eq(WorkflowNodeTrans::getWorkflowId,workflowInfo.getWorkflowId())
            );
            workflowNodeTransConditionService.update(
                    new LambdaUpdateWrapper<WorkflowNodeTransCondition>().set(WorkflowNodeTransCondition::getDeletedFlg, FebsConstant.INVALID)
                            .eq(WorkflowNodeTransCondition::getWorkflowId,workflowInfo.getWorkflowId())
            );
            workflowNodeReviewService.update(
                    new LambdaUpdateWrapper<WorkflowNodeReview>().set(WorkflowNodeReview::getDeletedFlg, FebsConstant.INVALID)
                            .eq(WorkflowNodeReview::getWorkflowId,workflowInfo.getWorkflowId())
            );
            workflowNodeButtonService.update(
                    new LambdaUpdateWrapper<WorkflowNodeButton>().set(WorkflowNodeButton::getDeletedFlg, FebsConstant.INVALID)
                            .eq(WorkflowNodeButton::getWorkflowId,workflowInfo.getWorkflowId())
            );
            resolveWorkflowJson(workflowDesignJson,workflowInfo);
        }
        //#endregion
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    public void resolveWorkflowJson(String workflowDesignJson, WorkflowInfo workflowInfo) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        WorkFlowJsonDto workFlowDesignModel = mapper.readValue(workflowDesignJson, WorkFlowJsonDto.class);
        WorkflowNode workflowNode = new WorkflowNode();
        if(WorkflowConstant.START_NODE.equals(workFlowDesignModel.getType())){
            String uuid = workFlowDesignModel.getUuid();
            workflowNode = getDesignNodeByUuid(workflowInfo.getWorkflowId(), uuid);
            if(null!=workflowNode && null!=workflowNode.getWorkflowNodeId()){
                //已有的节点重置启用
                workflowNode.setDeletedFlg(FebsConstant.VALID);
                workflowNode.setUpdatedBy(workflowInfo.getUpdatedBy());
                workflowNode.setUpdatedTime(workflowInfo.getUpdatedTime());
            }else{
                workflowNode.setCreatedBy(workflowInfo.getCreatedBy());
                workflowNode.setCreatedTime(workflowInfo.getCreatedTime());
            }
            workflowNode.setTenantId(workflowInfo.getTenantId());
            workflowNode.setBusinessNodeId("");
            workflowNode.setBusinessNodeName("");
            workflowNode.setWorkflowId(workflowInfo.getWorkflowId());
            workflowNode.setWorkflowNodeName(workFlowDesignModel.getType());
            workflowNode.setWorkflowNodeUuid(workFlowDesignModel.getUuid());
            workflowNode.setWorkflowNodeType(workFlowDesignModel.getType());
            workflowNodeService.saveOrUpdate(workflowNode);

            Map<String,WorkflowNode> designNodeMap=new HashMap<>();
            designNodeMap.put(uuid,workflowNode);
            WorkFlowJsonDto childNode = workFlowDesignModel.getChildNode();
            if(null!=childNode){
                resolveWorkflowNodeChildren(childNode,null,workflowNode,designNodeMap,new HashMap<>(),workflowInfo.getTenantId());
            }
        }
    }

    private WorkflowNode getDesignNodeByUuid(String workflowId,String uuid){
        WorkflowNode parDesignNode=new WorkflowNode();
        parDesignNode.setWorkflowId(workflowId);
        parDesignNode.setWorkflowNodeUuid(uuid);
        WorkflowNode workflowDesignNode = workflowNodeService.findWorkflowNode(parDesignNode);
        if(null!=workflowDesignNode &&  null != workflowDesignNode.getWorkflowNodeId()){
            return workflowDesignNode;
        }
        return new WorkflowNode();
    }

    /***
     *
     * @param childNode 当前节点
     * @param endNode   条件节点平级节点
     * @param prevNode 当前节点上级节点
     * @param designNodeMap 节点Map
     * @param checkUserMap 审核、抄送节点指定人员、角色、部门Map
     * @throws JsonProcessingException
     */
    @Transactional(rollbackFor = Exception.class)
    public void resolveWorkflowNodeChildren(WorkFlowJsonDto childNode, WorkFlowJsonDto endNode, WorkflowNode prevNode, Map<String,WorkflowNode> designNodeMap, Map<String,Object> checkUserMap, String tenantId){

        if(null!=childNode && null!=childNode.getNodeId() && StringUtils.isNotBlank(childNode.getNodeId())){
            WorkflowNode workflowNode= new WorkflowNode();
            String uuid = childNode.getUuid();
            if(!designNodeMap.containsKey(uuid)){
                workflowNode = getDesignNodeByUuid(prevNode.getWorkflowId(), uuid);
            }else{
                workflowNode = designNodeMap.get(uuid);
            }
            //region 组装上级节点流向当前节点
            WorkflowNodeTrans workflowNodeTrans=new WorkflowNodeTrans();
            workflowNodeTrans.setWorkflowId(prevNode.getWorkflowId());
            workflowNodeTrans.setCreatedBy(prevNode.getCreatedBy());
            workflowNodeTrans.setCreatedTime(prevNode.getCreatedTime());
            WorkFolwJsonPropertie properties = childNode.getProperties();
            //endregion
            if(null != workflowNode && null != workflowNode.getWorkflowNodeId()){
                workflowNode.setDeletedFlg(FebsConstant.VALID);
            }else{
                workflowNode.setCreatedBy(prevNode.getCreatedBy());
                workflowNode.setCreatedTime(prevNode.getCreatedTime());
            }
            workflowNode.setWorkflowId(prevNode.getWorkflowId());
            workflowNode.setBusinessNodeId(properties.getBusinessNodeId());
            workflowNode.setBusinessNodeName(properties.getBusinessNodeName());
            workflowNode.setWorkflowNodeUuid(childNode.getUuid());
            workflowNode.setWorkflowNodeType(childNode.getType());
            workflowNode.setTenantId(tenantId);
            workflowNode.setRemark(properties.getRemark());
            if(WorkflowConstant.APPROVE_NODE.equals(childNode.getType())){
                //审核节点
                //region保存当前节点
                workflowNode.setWorkflowNodeName(childNode.getName());
                workflowNode.setWorkflowNodeCheckType(properties.getWorkflowNodeCheckType());
                workflowNode.setNodeBeforeReviewFlg(properties.getNodeBeforeReviewFlg());
                workflowNode.setNodeModifyFlg(properties.getNodeModifyFlg());
                workflowNodeService.saveOrUpdate(workflowNode);
                designNodeMap.put(uuid,workflowNode);

                //保存审核权限
                if("1".equals(properties.getWorkflowNodeCheckType()) || "3".equals(properties.getWorkflowNodeCheckType()) || "4".equals(properties.getWorkflowNodeCheckType()) || "5".equals(properties.getWorkflowNodeCheckType())){
                    List<WorkflowNodeReviewPropertyDto> workflowNodeCheckUserList = properties.getWorkflowNodeCheckUserList();
                    if(null!=workflowNodeCheckUserList &&workflowNodeCheckUserList.size()>0){
                        for (WorkflowNodeReviewPropertyDto nodeCheckUser:workflowNodeCheckUserList) {
                            String checkUserKey=WorkflowConstant.APPROVE_NODE+workflowNode.getWorkflowId()+"-"+workflowNode.getWorkflowNodeId()+"-"+nodeCheckUser.getWorkflowNodeCheckType()+"-"+nodeCheckUser.getWorkflowNodeCheckIds();
                            if(checkUserMap.containsKey(checkUserKey)){
                                continue;
                            }
                            WorkflowNodeReview workflowNodeReview = new WorkflowNodeReview();
                            workflowNodeReview.setWorkflowId(workflowNode.getWorkflowId());
                            workflowNodeReview.setTenantId(tenantId);
                            workflowNodeReview.setWorkflowNodeId(workflowNode.getWorkflowNodeId());
                            workflowNodeReview.setWorkflowNodeCheckType(nodeCheckUser.getWorkflowNodeCheckType());
                            workflowNodeReview.setWorkflowNodeCheckIds(nodeCheckUser.getWorkflowNodeCheckIds());
                            workflowNodeReview.setCreatedBy(workflowNode.getCreatedBy());
                            workflowNodeReview.setCreatedTime(workflowNode.getCreatedTime());
                            checkUserMap.put(checkUserKey,workflowNodeReview);
                            workflowNodeReviewService.save(workflowNodeReview);
                        }
                    }

                }
                //endregion
                //region保存对应流程节点按钮
                if(properties.getBtnSetList().size()>0){
                    //根据流程节点nodeId查询对应的节点下面有没有按钮
                    LambdaQueryWrapper<WorkflowNodeButton> wrapper=new LambdaQueryWrapper<>();
                    wrapper.eq(WorkflowNodeButton::getDeletedFlg,FebsConstant.VALID);
                    wrapper.eq(WorkflowNodeButton::getWorkflowNodeId,workflowNode.getWorkflowNodeId());
                    List<WorkflowNodeButton> workflowNodeButtonList = workflowNodeButtonService.list(wrapper);
                    //有按钮就全部删除
                    if(workflowNodeButtonList.size()>0){
                        for (WorkflowNodeButton workflowNodeButton:workflowNodeButtonList) {
                            workflowNodeButton.setDeletedFlg("1");
                            workflowNodeButton.setDeletedTime(new Date());
                            workflowNodeButtonService.updateById(workflowNodeButton);
                        }
                    }
                    //将前端传过来的按钮集合插入
                    for (WorkflowNodeButton workflowNodeButtons:properties.getBtnSetList()) {
                        WorkflowNodeButton workflowNodeButton=new WorkflowNodeButton();
                        BeanUtils.copyProperties(workflowNodeButtons,workflowNodeButton);
                        workflowNodeButton.setWorkflowNodeButtonId(UUID.randomUUID().toString().replaceAll("-",""));
                        workflowNodeButton.setTenantId(tenantId);
                        workflowNodeButton.setWorkflowId(workflowNode.getWorkflowId());
                        workflowNodeButton.setWorkflowNodeId(workflowNode.getWorkflowNodeId());
                        workflowNodeButton.setCreatedTime(new Date());
                        workflowNodeButton.setDeletedFlg(FebsConstant.VALID);
                        workflowNodeButtonService.save(workflowNodeButton);
                    }
                }
                //endregion
                //region 保存上级节点流向当前节点
                workflowNodeTrans.setTenantId(tenantId);
                workflowNodeTrans.setWorkflowNodeTransFlg("1");
                workflowNodeTrans.setWorkflowNodeFromId(prevNode.getWorkflowNodeId());
                workflowNodeTrans.setWorkflowNodeToId(workflowNode.getWorkflowNodeId());
                workflowNodeTrans.setCreatedTime(new Date());
                workflowNodeTransService.save(workflowNodeTrans);
                //endregion
                //region 进入子节点
                WorkFlowJsonDto nextNode = childNode.getChildNode();
                if(null!=nextNode && null!=nextNode.getNodeId()){
                    resolveWorkflowNodeChildren(nextNode,endNode,workflowNode,designNodeMap,checkUserMap,tenantId);
                }else{
                    resolveWorkflowNodeChildren(endNode,null,workflowNode,designNodeMap,checkUserMap,tenantId);
                }
                //endregion
            }else if(WorkflowConstant.NOTIFIER_NODE.equals(childNode.getType())){
                //抄送节点
                //region保存当前节点
                workflowNode.setWorkflowNodeName(childNode.getName());
                workflowNodeService.saveOrUpdate(workflowNode);
                designNodeMap.put(uuid,workflowNode);
                //保存抄送人员权限
                List<WorkflowNodeReviewPropertyDto> workflowNodeCheckUserList = properties.getWorkflowNodeCheckUserList();
                if(null!=workflowNodeCheckUserList && workflowNodeCheckUserList.size()>0){
                    for ( WorkflowNodeReviewPropertyDto nodeCheckUser:workflowNodeCheckUserList) {
                        String checkUserKey=WorkflowConstant.NOTIFIER_NODE+workflowNode.getWorkflowId()+"-"+workflowNode.getWorkflowNodeId()+"-"+nodeCheckUser.getWorkflowNodeCheckType()+"-"+nodeCheckUser.getWorkflowNodeCheckIds();
                        if(checkUserMap.containsKey(checkUserKey)){
                            continue;
                        }
                        WorkflowNodeReview workflowNodeCheckUser = new WorkflowNodeReview();
                        workflowNodeCheckUser.setTenantId(tenantId);
                        workflowNodeCheckUser.setWorkflowId(workflowNode.getWorkflowId());
                        workflowNodeCheckUser.setWorkflowNodeId(workflowNode.getWorkflowNodeId());
                        workflowNodeCheckUser.setWorkflowNodeCheckType(nodeCheckUser.getWorkflowNodeCheckType());
                        workflowNodeCheckUser.setWorkflowNodeCheckIds(nodeCheckUser.getWorkflowNodeCheckIds());

                        workflowNodeCheckUser.setCreatedBy(workflowNode.getCreatedBy());
                        workflowNodeCheckUser.setCreatedTime(workflowNode.getCreatedTime());
                        checkUserMap.put(checkUserKey,workflowNodeCheckUser);
                        workflowNodeReviewService.save(workflowNodeCheckUser);
                    }
                }
                //endregion
                //region 保存上级节点流向当前节点
                workflowNodeTrans.setTenantId(tenantId);
                workflowNodeTrans.setWorkflowNodeTransFlg("1");
                workflowNodeTrans.setWorkflowNodeFromId(prevNode.getWorkflowNodeId());
                workflowNodeTrans.setWorkflowNodeToId(workflowNode.getWorkflowNodeId());
                workflowNodeTrans.setCreatedTime(new Date());
                workflowNodeTransService.save(workflowNodeTrans);
                //endregion
                //region 进入子节点
                WorkFlowJsonDto nextNode = childNode.getChildNode();
                if(null!=nextNode && null!=nextNode.getNodeId()){
                    resolveWorkflowNodeChildren(nextNode,endNode,workflowNode,designNodeMap,checkUserMap,tenantId);
                }else{
                    resolveWorkflowNodeChildren(endNode,null,workflowNode,designNodeMap,checkUserMap,tenantId);
                }
                //endregion
            }else if(WorkflowConstant.CONDITION_NODE.equals(childNode.getType())){
                //条件节点
                //region 保存当前节点

                workflowNode.setWorkflowNodeName(childNode.getName());
                workflowNodeService.saveOrUpdate(workflowNode);
                designNodeMap.put(uuid,workflowNode);
                //endregion
                //region 保存上级节点流向当前节点
                workflowNodeTrans.setTenantId(tenantId);
                workflowNodeTrans.setWorkflowNodeTransFlg("2");
                workflowNodeTrans.setWorkflowNodeFromId(prevNode.getWorkflowNodeId());
                workflowNodeTrans.setWorkflowNodeToId(workflowNode.getWorkflowNodeId());
                workflowNodeTrans.setWorkflowNodeTransType(properties.getWorkflowNodeTransType());
                if(null!=properties.getWorkflowNodeTransOrder() && StringUtils.isNotBlank(properties.getWorkflowNodeTransOrder())){
                    workflowNodeTrans.setWorkflowNodeTransOrder(Integer.valueOf(properties.getWorkflowNodeTransOrder()));
                }
                workflowNodeTransService.save(workflowNodeTrans);
                //endregion
                //region 保存条件节点流向条件
                WorkFolwJsonPropertie nextProperty = childNode.getProperties();
                List<WorkflowNodeConditionPropertyDto> workflowNodeTransConditionList = nextProperty.getWorkflowNodeTransConditionList();
                if(null!=workflowNodeTransConditionList && workflowNodeTransConditionList.size()>0){
                    for (WorkflowNodeConditionPropertyDto nodeConditionProperty:workflowNodeTransConditionList) {
                        List<WorkflowNodeConditionPropertyDetailDto> workflowNodeTransConditions = nodeConditionProperty.getWorkflowNodeTransConditions();
                        for (WorkflowNodeConditionPropertyDetailDto nodeConditionPropertyDetail:workflowNodeTransConditions) {
                            WorkflowNodeTransCondition workflowNodeTransCondition = new WorkflowNodeTransCondition();
                            workflowNodeTransCondition.setTenantId(tenantId);
                            workflowNodeTransCondition.setWorkflowId(prevNode.getWorkflowId());
                            workflowNodeTransCondition.setWorkflowNodeTransId(workflowNodeTrans.getWorkflowNodeTransId());
                            workflowNodeTransCondition.setConditionType(nodeConditionPropertyDetail.getConditionType());
                            workflowNodeTransCondition.setConditionKey(nodeConditionPropertyDetail.getConditionKey());
                            workflowNodeTransCondition.setConditionOperation(nodeConditionPropertyDetail.getConditionOperation());
                            workflowNodeTransCondition.setConditionValue(nodeConditionPropertyDetail.getConditionValue());
                            workflowNodeTransCondition.setBusinessTable(nodeConditionPropertyDetail.getBusinessTable());
                            workflowNodeTransCondition.setCreatedTime(new Date());
                            workflowNodeTransConditionService.save(workflowNodeTransCondition);
                        }
                    }
                }
                //endregion
                //region 进入子节点
                WorkFlowJsonDto nextNode = childNode.getChildNode();
                if(null!=nextNode && null!=nextNode.getNodeId()){
                    resolveWorkflowNodeChildren(nextNode,endNode,workflowNode,designNodeMap,checkUserMap,tenantId);
                }else{
                    resolveWorkflowNodeChildren(endNode,null,workflowNode,designNodeMap,checkUserMap,tenantId);
                }
                //endregion
            }else if(WorkflowConstant.ROUTE_NODE.equals(childNode.getType())){
                //条件虚拟父节点
                List<WorkFlowJsonDto> conditionNodes = childNode.getConditionNodes();
                WorkFlowJsonDto nextNode = childNode.getChildNode();
                if(null==nextNode && null != endNode){
                    nextNode=endNode;
                }else if(null != endNode){
                    nextNode=continueChildNode(nextNode,endNode);
                }
                //region保存当前节点
                workflowNode.setWorkflowNodeName(childNode.getType());
                workflowNodeService.saveOrUpdate(workflowNode);
                designNodeMap.put(uuid,workflowNode);
                //endregion
                //region 保存上级节点流向当前节点
                workflowNodeTrans.setTenantId(tenantId);
                workflowNodeTrans.setWorkflowNodeTransFlg("1");
                workflowNodeTrans.setWorkflowNodeFromId(prevNode.getWorkflowNodeId());
                workflowNodeTrans.setWorkflowNodeToId(workflowNode.getWorkflowNodeId());
                workflowNodeTransService.save(workflowNodeTrans);
                //endregion
                for (WorkFlowJsonDto conditionNode: conditionNodes) {
                    if(null!=conditionNode && null!=conditionNode.getNodeId()){
                        resolveWorkflowNodeChildren(conditionNode,nextNode,workflowNode,designNodeMap,checkUserMap,tenantId);
                    }
                }
            }
        }
    }

    private WorkFlowJsonDto continueChildNode(WorkFlowJsonDto node, WorkFlowJsonDto endNode){
        WorkFlowJsonDto childNode = node.getChildNode();
        //String type = node.getType();
        if(null!=childNode){
            childNode=continueChildNode(childNode,endNode);
            node.setChildNode(childNode);
        }else{
            node.setChildNode(endNode);
        }
        return node;
    }
}
